package org.real.guetshop.controller.user;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.real.guetshop.pojo.dto.AddressDTO;
import org.real.guetshop.pojo.vo.AddressVO;
import org.real.guetshop.result.Result;
import org.real.guetshop.service.AddressService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/user/addr")
public class AddressController {

    @Resource
    private AddressService addressService;

    /**
     * 用户添加收货地址
     * @param addressDTO
     * @return
     */
    @PostMapping("/newAddr")
    public Result newAddr(@RequestBody AddressDTO addressDTO){
        log.info("添加收货地址：{}",addressDTO);
        addressService.addNewAddress(addressDTO);
        return Result.success();
    }

    /**
     * 用户获取收货地址列表
     * @return
     */
    @GetMapping("/getAddr")
    public Result<List<AddressVO>> getAddr(){
        log.info("用户获取收货地址");
        return Result.success(addressService.getAddrById());
    }

    /**
     * 用户编辑收获地址
     * @param addressDTO
     * @return
     */
    @PutMapping("/editAddr")
    public Result updateAddr(@RequestBody AddressDTO addressDTO){
        log.info("用户编辑收货地址：{}",addressDTO);
        addressService.editAddr(addressDTO);
        return Result.success();
    }

    /**
     * 用户设置默认地址
     * @param addressDTO
     * @return
     */
    @PutMapping("/setDeAddr")
    public Result setDefaultAddr(@RequestBody AddressDTO addressDTO){
        log.info("用户设置默认地址：{}",addressDTO);
        addressService.setDefaultAddr(addressDTO);
        return Result.success();
    }

    /**
     * 用户删除地址
     * @param addressDTO
     * @return
     */
    @DeleteMapping("/deleteAddr")
    public Result deleteAddr(@RequestBody AddressDTO addressDTO){
        log.info("用户删除地址：{}",addressDTO);
        addressService.deleteAddr(addressDTO);
        return Result.success();
    }
}
